package com.zm.demo.core.collection;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author z01140
 * @Package: com.zm.demo.core.collection
 * @Description:
 * @CreateDate 2021/10/27 17:04
 * @UpdateDate 2021/10/27 17:04
 */
public class TreeMapTest {

    public static void main(String[] args) {
        TreeMap<String, Integer> treeMap= new TreeMap<>(String::compareTo);
        HashMap<String, Integer> hashMap= new HashMap<>();

        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            hashMap.put(r.nextInt(90)+10+"",i );
        }
        treeMap.putAll(hashMap);
//        System.out.println(hashMap);
//        System.out.println(treeMap);
//
//        hashMap.keySet().forEach(k -> System.out.print(k + " "));
//        System.out.println();
//        treeMap.keySet().forEach(k -> System.out.print(k + " "));

        AtomicInteger i = new AtomicInteger(0);
        try {
            hashMap.keySet().forEach(s ->{
//                if (i.incrementAndGet() == 3){
//                    throw new RuntimeException("1111");
//                }else {
                    treeMap.put(s, i.get());
                    System.out.print(s + " ");
//                }
            });
            System.out.println();
            hashMap.keySet().stream().sorted().forEach(s ->{
//                if (i.incrementAndGet() == 3){
//                    throw new RuntimeException("1111");
//                }else {
                    treeMap.put(s, i.get());
                    System.out.print(s + " ");
//                }
            });
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
